﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace fap
{
  
    public class 相机坐标data : INotifyPropertyChanged
    {

        private string _name;
        private string _X;
        private string _Y;
        private string _Z;
        private string _光源;
        private string _间距;
        public string 间距
        {
            get { return _间距; }
            set { this._间距 = value; OnPropertyChanged("间距"); }
        }
        public string 光源
        {
            get { return _光源; }
            set { this._光源 = value; OnPropertyChanged("光源"); }
        }
        public string Name
        {
            get { return _name; }
            set { this._name = value; OnPropertyChanged("Name"); }
        }

        public string X
        {
            get { return this._X; }
            set
            {
                this._X = value;
                OnPropertyChanged("X");
            }
        }
        public string Y
        {
            get { return this._Y; }
            set
            {
                this._Y = value;
                OnPropertyChanged("Y");
            }
        }
        public string Z
        {
            get { return this._Z; }
            set
            {
                this._Z = value;
                OnPropertyChanged("Z");
            }
        }

      
        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

    }
    public class 纵轴坐标data : INotifyPropertyChanged
    {

        private string _name;
    
        private string _Y;
        private string _Z;





        public string Name
        {
            get { return _name; }
            set { this._name = value; OnPropertyChanged("Name"); }
        }

      
        public string Y
        {
            get { return this._Y; }
            set
            {
                this._Y = value;
                OnPropertyChanged("Y");
            }
        }
        public string Z
        {
            get { return this._Z; }
            set
            {
                this._Z = value;
                OnPropertyChanged("Z");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

    }
    public class 夹料坐标data : INotifyPropertyChanged
    {

       

        private string _入料位置;
        private string _出料位置;





        public string 入料位置
        {
            get { return _入料位置; }
            set { this._入料位置 = value; OnPropertyChanged("入料位置"); }
        }


        public string 出料位置
        {
            get { return this._出料位置; }
            set
            {
                this._出料位置 = value;
                OnPropertyChanged("出料位置");
            }
        }
       
       

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

    }
    public class 运动参数data : INotifyPropertyChanged
    {

        private string _name;

        private string _Value;
     


        public string Name
        {
            get { return _name; }
            set { this._name = value; OnPropertyChanged("Name"); }
        }


        public string Value
        {
            get { return this._Value; }
            set
            {
                this._Value = value;
                OnPropertyChanged("Y");
            }
        }



        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

    }

  
   

    /// <summary>
    /// DataTable与实体类互相转换
    /// </summary>
    /// <typeparam name="T">实体类</typeparam>
    public class ModelHandler<T> where T : new()
        {
            #region DataTable转换成实体类

            /// <summary>
            /// 填充对象列表：用DataSet的第一个表填充实体类
            /// </summary>
            /// <param name="ds">DataSet</param>
            /// <returns></returns>
            public List<T> FillModel(DataSet ds)
            {
                if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    return FillModel(ds.Tables[0]);
                }
            }

            /// <summary>  
            /// 填充对象列表：用DataSet的第index个表填充实体类
            /// </summary>  
            public List<T> FillModel(DataSet ds, int index)
            {
                if (ds == null || ds.Tables.Count <= index || ds.Tables[index].Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    return FillModel(ds.Tables[index]);
                }
            }

            /// <summary>  
            /// 填充对象列表：用DataTable填充实体类
            /// </summary>  
            public List<T> FillModel(DataTable dt)
            {
                if (dt == null || dt.Rows.Count == 0)
                {
                    return null;
                }
                List<T> modelList = new List<T>();
                foreach (DataRow dr in dt.Rows)
                {
                    //T model = (T)Activator.CreateInstance(typeof(T));  
                    T model = new T();
                    for (int i = 0; i < dr.Table.Columns.Count; i++)
                    {
                        PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
                        if (propertyInfo != null && dr[i] != DBNull.Value)
                    {  try
                        {
                            propertyInfo.SetValue(model, dr[i], null);

                        }
                        catch
                        {
                            propertyInfo.SetValue(model,Convert.ToDouble( dr[i]), null);

                        }




                    }
                    }

                    modelList.Add(model);
                }
                return modelList;
            }

            /// <summary>  
            /// 填充对象：用DataRow填充实体类
            /// </summary>  
            public T FillModel(DataRow dr)
            {
                if (dr == null)
                {
                    return default(T);
                }

                //T model = (T)Activator.CreateInstance(typeof(T));  
                T model = new T();

                for (int i = 0; i < dr.Table.Columns.Count; i++)
                {
                    PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
                    if (propertyInfo != null && dr[i] != DBNull.Value)
                        propertyInfo.SetValue(model, dr[i], null);
                }
                return model;
            }

            #endregion

            #region 实体类转换成DataTable

            /// <summary>
            /// 实体类转换成DataSet
            /// </summary>
            /// <param name="modelList">实体类列表</param>
            /// <returns></returns>
            public DataSet FillDataSet(List<T> modelList)
            {
                if (modelList == null || modelList.Count == 0)
                {
                    return null;
                }
                else
                {
                    DataSet ds = new DataSet();
                    ds.Tables.Add(FillDataTable(modelList));
                    return ds;
                }
            }

            /// <summary>
            /// 实体类转换成DataTable
            /// </summary>
            /// <param name="modelList">实体类列表</param>
            /// <returns></returns>
            public DataTable FillDataTable(List<T> modelList)
            {
                if (modelList == null || modelList.Count == 0)
                {
                    return null;
                }
                DataTable dt = CreateData(modelList[0]);

                foreach (T model in modelList)
                {
                    DataRow dataRow = dt.NewRow();
                    foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                    {
                        dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
                    }
                    dt.Rows.Add(dataRow);
                }
                return dt;
            }

            /// <summary>
            /// 根据实体类得到表结构
            /// </summary>
            /// <param name="model">实体类</param>
            /// <returns></returns>
            private DataTable CreateData(T model)
            {
                DataTable dataTable = new DataTable(typeof(T).Name);
                foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                {
                    dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
                }
                return dataTable;
            }

            #endregion
        }


   


}
